package com.clsfd;

import java.io.*;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.log4j.*;

import com.clsfd.database.DatabaseConnection;
import com.clsfd.database.Subscription;
import com.clsfd.database.VehicleAlert;
import com.clsfd.database.LandAlert;
import com.clsfd.database.Alert;
import com.clsfd.database.Advert;
import com.clsfd.sms.SMSHandler;
import com.clsfd.database.SystemParams;
import com.logging.DcLogFormatter;

public class AlertSender {
		
	public static void main (String args[]) throws Exception{
	
		DatabaseConnection sql= new DatabaseConnection(null);
		SystemParams syspara = new SystemParams(sql);
		SMSHandler smscon = new SMSHandler(syspara);
		String origination = syspara.getOriginationAddress();
		Logger Log=null;
		
		FileHandler filehandler = new FileHandler(syspara.getSmsHandlerLogFile()+"%g.log",1000000000,3,true);
		Formatter formatterTxt=new DcLogFormatter();
		filehandler.setFormatter(formatterTxt);
		Log = Logger.getLogger("SmsHandler");
		Log.addHandler(filehandler);
		Log.setLevel(Level.parse(syspara.getSmsHandlerLogLevel()));
		
		
		 Log.setLevel(Level.INFO);		  
		  
		  Log.info("AlertSender: Starting Alert sender job");
		 
		//Subscription customer = new Subscription(sql, 0, null, null, null, 0, null, 0, null, null, 0, 0);

		Collection<Alert> alertcollection=sql.getActiveAlerts(2);
		Iterator<Alert> alerts=alertcollection.iterator();
		
		while(alerts.hasNext()){
				Alert vv=alerts.next();
			System.out.println("test");
			Collection<Subscription> subscribercollection =sql.getActiveSubscriptions(vv, 1000)	;
			Iterator<Subscription> subscriber =subscribercollection.iterator();
			int alertid = vv.getAlertID();
			Log.info("AlertSender: Active Alert ID " + alertid);
			System.out.println(alertid);
			
			while(subscriber.hasNext()){
				
				System.out.println("test2");
				
			Subscription sub = subscriber.next();
			System.out.println("service" +sub.getServiceitem());
			String DA = sub.getMsisdn();
			String language = sub.getLanguage();
			
			String message ;
				if (language == "Si"){
					 message = vv.getSmssin();
					 
					} else if (language == "Ta"){
						 message = vv.getSmstam();	
					}else {
						
					 message = vv.getSmseng();	
					}	
							
				//write for sending sms	
				
				String respon = smscon.SendTextMessage(DA, origination , message).getResponse();
				
						String[] respond = respon.split(":");
						if(respond[0].contains("0")){
						System.out.println(respond[0]);
				Log.info("AlertSender: Active customer=" + DA + " alert message sent=" +  message);
				sub.setLastAlertID(alertid);
						}else {
				
					Log.warning("AlertSender: Active customer=" + DA + " alert message sent failed=" +  message);	
					//smscon.SendTextMessage(DA, origination , message);
					
						}
				
			//Update the user
			
			
			
				//System.out.println(sub.getLanguage());
				
			}
		
			//System.out.println(vv.getAlertID());
			
			
			//vv.setExpiretedate("2012-12-13");
			
		}
		
		
		
		
		
	
		
	}
}
